using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using SMSManager.Commn.Interfaces;
using SMSManager.Commn.Interfaces.ViewModels;
using SMSManager.Data;
using SMSManager.WpfClient.Properties;

namespace SMSManager.WpfClient.ViewModels
{
	[Export(typeof (ILoginViewModel))]
	[PartCreationPolicy(CreationPolicy.NonShared)]
	public class LoginViewModel : NotifyPropertyChangedBase, ILoginViewModel
	{
		private readonly CompositionContainer _container;
		private ISMSService _smsService;
		private IUser _user;

		[ImportingConstructor]
		public LoginViewModel(CompositionContainer container)
		{
			_container = container;
			_user = new User();
			LoginMessage = "";

			LoginCommand = new DelegateCommand(Login);
		}

		private void ActivateView()
		{
			var regionManager = _container.GetExportedValue<IRegionManager>();
			IRegion region = regionManager.Regions[Resources.SMSServiceRegionName];
			if (_smsService.IsLogin)
			{
				object view = region.GetView(Resources.SMSSendViewName);
				region.Activate(view);

				LoginMessage = "";
				this.OnPropertyChanged(x => x.LoginMessage);
			}
			else
			{
				/*var view = region.GetView(Resources.LoginViewName);
				region.Activate(view);*/

				LoginMessage = Resources.LoginFailMessage;
				this.OnPropertyChanged(x => x.LoginMessage);
			}
		}

		private void Login()
		{
			_smsService.Login(_user);

			this.OnPropertyChanged(x => x.IsLogin);

			ActivateView();
		}

		private void UserPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			switch (e.PropertyName)
			{
				case "IsRemember":
					this.OnPropertyChanged(x => x.IsRemember);
					return;
				case "IsAutoLogin":
					this.OnPropertyChanged(x => x.IsAutoLogin);
					return;
			}
		}

		public ISMSService SMSService
		{
			set { _smsService = value; }
		}

		public IUser User
		{
			set
			{
				if (value == null) return;

				_user.PropertyChanged -= UserPropertyChanged;
				_user = value;
				_user.PropertyChanged += UserPropertyChanged;
			}
		}

		public string Id
		{
			get { return _user.Id; }
			set { _user.Id = value; }
		}

		public bool IsAutoLogin
		{
			get { return _user.IsAutoLogin; }
			set { _user.IsAutoLogin = value; }
		}

		public bool IsLogin
		{
			get { return _smsService.IsLogin; }
		}

		public bool IsRemember
		{
			get { return _user.IsRemember; }
			set { _user.IsRemember = value; }
		}

		public ICommand LoginCommand { get; private set; }

		public string LoginMessage { get; private set; }

		public string Password
		{
			get { return _user.Password; }
			set { _user.Password = value; }
		}

		public string SMSServiceName
		{
			get { return _smsService.Name; }
		}


		public string this[string propertyName]
		{
			get { return _user[propertyName]; }
		}

		public string Error
		{
			get { throw new NotSupportedException(); }
		}
	}
}